package org.jeecg.modules.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.jeecg.common.modules.dic.vo.GoodResponseVo;
import org.jeecg.common.modules.dic.entity.DicBeGoodAt;
import org.jeecg.common.modules.dic.service.IDicBeGoodAtService;
import org.jeecg.common.modules.doctor.entity.Doctor;
import org.jeecg.common.modules.doctor.service.IDoctorService;
import org.jeecg.common.util.enums.BusinessTypeEnum;
import org.jeecg.common.util.enums.CertificationStatusEnum;
import org.jeecg.common.util.result.Result;
import org.jeecg.common.util.result.ResultMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ApiGoodService {
    @Resource
    private IDicBeGoodAtService beGoodAtService;

    @Resource
    private IDoctorService doctorService;

    public Result<List<GoodResponseVo>> getAll(){

        LambdaQueryWrapper<Doctor> doctorQuery  = Wrappers.lambdaQuery();
        doctorQuery.select(Doctor::getDepartment);
        doctorQuery.eq(Doctor::getBusinessType, BusinessTypeEnum.DOCTOR.getId());
        doctorQuery.eq(Doctor::getCertificationStatus, CertificationStatusEnum.CERTIFICATION.getCode());
        doctorQuery.eq(Doctor::getIsDisplay,1);
        doctorQuery.groupBy(Doctor::getDepartment);
        List<Doctor> doctors = doctorService.list(doctorQuery);

        LambdaQueryWrapper<DicBeGoodAt> query = Wrappers.lambdaQuery();
        query.eq(DicBeGoodAt::getParentId,0);
        query.eq(DicBeGoodAt::getInvaild,0);
        query.in(DicBeGoodAt::getId,doctors.stream().map(item->item.getDepartment()).collect(Collectors.toList()));
        query.orderByAsc(DicBeGoodAt::getSort);
        List<DicBeGoodAt> goods = beGoodAtService.list(query);
        List<GoodResponseVo>  vos = new ArrayList<GoodResponseVo>();
        for (DicBeGoodAt good:goods) {
            LambdaQueryWrapper<DicBeGoodAt> queryWrapper = Wrappers.lambdaQuery();
            queryWrapper.eq(DicBeGoodAt::getParentId,good.getId());
            GoodResponseVo vo = new GoodResponseVo();
            vo.setName(good.getName());
            vo.setId(good.getId());
            vo.setImg(good.getImg());
            vo.setSort(good.getSort());
            vo.setList(beGoodAtService.list(queryWrapper));
            vos.add(vo);
        }
        return ResultMapper.<List<GoodResponseVo>>ok().setData(vos);
    }
    public List<DicBeGoodAt> getAllByDepartment(String departmentId){
        LambdaQueryWrapper<DicBeGoodAt> query = Wrappers.lambdaQuery();
        if("0".equals(departmentId)){
            query.eq(DicBeGoodAt::getParentId,departmentId);
        }else {
            query.eq(DicBeGoodAt::getId,departmentId);
        }

        List<DicBeGoodAt> goods = beGoodAtService.list(query);
//        List<GoodResponseVo>  vos = new ArrayList<GoodResponseVo>();
//        for (DicBeGoodAt good:goods) {
//            LambdaQueryWrapper<DicBeGoodAt> queryWrapper = Wrappers.lambdaQuery();
//            queryWrapper.eq(DicBeGoodAt::getParentId,good.getId());
//            GoodResponseVo vo = new GoodResponseVo();
//            vo.setName(good.getName());
//            vo.setId(good.getId());
//            vo.setImg(good.getImg());
//            vo.setSort(good.getSort());
//            vo.setList(beGoodAtService.list(queryWrapper));
//            vos.add(vo);
//        }
        return goods;
    }

    /**
     * 科室获取病种
     * @param departmentId
     * @return
     */
    public List<DicBeGoodAt> getAllByParentId(String departmentId){
        LambdaQueryWrapper<DicBeGoodAt> query = Wrappers.lambdaQuery();
//        if(departmentId==0){
//            query.eq(DicBeGoodAt::getParentId,departmentId);
//        }else {
//            query.eq(DicBeGoodAt::getId,departmentId);
//        }
        query.eq(DicBeGoodAt::getParentId,departmentId);
        query.ne(DicBeGoodAt::getInvaild,1);
        List<DicBeGoodAt> goods = beGoodAtService.list(query);
        List<GoodResponseVo>  vos = new ArrayList<GoodResponseVo>();
        for (DicBeGoodAt good:goods) {
            LambdaQueryWrapper<DicBeGoodAt> queryWrapper = Wrappers.lambdaQuery();
            queryWrapper.eq(DicBeGoodAt::getParentId,good.getId());
            queryWrapper.ne(DicBeGoodAt::getInvaild,1);
            GoodResponseVo vo = new GoodResponseVo();
            vo.setName(good.getName());
            vo.setId(good.getId());
            vo.setImg(good.getImg());
//            vo.setSort(good.getSort());
            vo.setList(beGoodAtService.list(queryWrapper));
            vos.add(vo);
        }
        return goods;
    }
}
